package com.ruoyi.system.util;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.StoreSettings;
import com.ruoyi.system.service.IStoreSettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class StoreIdUtils {
    @Autowired
    private  RedisCache redisCache;

    @Autowired
    private IStoreSettingsService storeSettingsService;

    /**
     * 获取门店id
     * @return
     */
    public Long getStoreId(){
//        redisCache.setCacheObject("4",4,60*60*1000, TimeUnit.MILLISECONDS);
//        Object cacheObject = redisCache.getCacheObject("4");
        Object cacheObject = redisCache.getCacheObject(SecurityUtils.getUserId().toString());
        if (AbdulEmptyUtils.isEmpty(cacheObject)){
            throw new RuntimeException("操作失败，请先绑定门店！！！");
        }
        return Long.valueOf(cacheObject.toString());
    }

    /**
     * 获取门店对象
     */
    public StoreSettings getStore(Long storeId){
        if (AbdulEmptyUtils.isEmpty(storeId)){
            throw new RuntimeException("操作失败，参数门店id不能为空！！！");
        }
        return storeSettingsService.getById(storeId);
    }
}
